Thread: why a[i] is not compiling . array compilation error

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    why a[i] is not compiling . array compilation error

    Hi,

    i tried to compile the below program

    1st program

    Code:
    int main()
    {
            int i=10;
            int a[2]={i,i+1};
            printf ("hai %d\n",a[0]);
            return 0;
    }

    2nd program
    Code:
    int main()
    {
            int i=10;
            int j=2;
            int a[j]={i,i+1};
            printf ("hai %d\n",a[0]);
            return 0;
    }
    Why i am able to compile first program and getting compile error for second program?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unless you are able to make use of the variable length array feature introduced in the 1999 edition of the C standard, your array a must have a fixed size known at compile time, so j cannot be a variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by laserlight View Post
    Unless you are able to make use of the variable length array feature introduced in the 1999 edition of the C standard, your array a must have a fixed size known at compile time, so j cannot be a variable.
    Translation: You are still using Turbo C!


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi,

    is it variable length array features is compiler dependent?

    I am using the gcc compiler version (gcc (GCC) 3.4.5 20051201 (Red Hat 3.4.5-2)). my compiler will support variable length array feature?

    can you let me know small c program with variable length array feature?

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You can compile with compile flag: -std=c99

  6. #6
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Quote Originally Posted by laserlight View Post
    Unless you are able to make use of the variable length array feature introduced in the 1999 edition of the C standard, your array a must have a fixed size known at compile time, so j cannot be a variable.
    Hi light,

    I did not get properlly "Unless you are able to make use of the variable length array feature introduced in the 1999 edition of the C standard".

    please can you tell once this?

    Regards
    NKRao

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nkrao123@gmail. View Post
    please can you tell once this?
    I think we already have at least two or three times. The "variable length" feature in c99 -- the 1999 ANSI C standard -- allows you to declare an array with a variable length.

    However, no C standard allows you to initialize such an object with values. You cannot do that, period, end of story.

    Here are two declarations:
    Code:
    int x;
    int array[n];
    These variables have not been initialized; they could have any value in them.

    Code:
    x = 12;
    Now x has been initialized. It could have been initialized in the declaration:

    Code:
    int x = 12;
    An array of fixed length can also be initialized in its declaration:
    Code:
    int array[3] = {4, 5, 666};
    However, you cannot:
    1) initialize an array after its declaration; you must assign to individual elements.*
    2) initialize a variable length array, such as "int array[n]"

    Read posts #14 and #15 again if you still do not understand what this means. If you want to put values into a variable length array, you have to do so after the declaration.

    * actually you can set more than one at time with memset().
    Last edited by MK27; 09-06-2011 at 05:40 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nkrao123@gmail.
    is it variable length array features is compiler dependent?
    In theory, no. In practice, yes, because not all C compilers in present use conform sufficiently to C99.

    Quote Originally Posted by nkrao123@gmail.
    I am using the gcc compiler version (gcc (GCC) 3.4.5 20051201 (Red Hat 3.4.5-2)). my compiler will support variable length array feature?
    It should. You may need to pass -std=c99 as a compiler option.

    Quote Originally Posted by nkrao123@gmail.
    can you let me know small c program with variable length array feature?
    Your second program is such an example program, except that you cannot initialise the array like that. Oh and remember to #include <stdio.h>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    kotin
    Join Date
    Oct 2009
    Posts
    132
    It should. You may need to pass -std=c99 as a compiler option.
    Light used that word as it should. I thought my compiler may have that features. That why i tried. Now i understand that my compilr does not supporting this feature

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nkrao123@gmail. View Post
    Now i understand that my compilr does not supporting this feature
    No, no you don't. You don't understand at all, because you aren't reading what people are telling you over and over.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nkrao123@gmail. View Post
    Light used that word as it should. I thought my compiler may have that features. That why i tried. Now i understand that my compilr does not supporting this feature
    Sorry, partially my mistake. As per laserlight's post above, what c99 will allow is this:

    Code:
    int array[x];
    Where x may be determined at runtime. Which means you cannot initialize it to any value, ie.

    Code:
    int array[x] = {1, 2, 3};
    is no good. You can use a "variable-sized object" but you can't initialize it in the declaration.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    And because you still won't understand what they're saying:
    Quote Originally Posted by MK27 View Post
    Which means you cannot initialize it to any value, ie.

    Code:
    int array[x] = {1, 2, 3};
    is no good. You can use a "variable-sized object" but you can't initialize it in the declaration.
    You can do what is in green.
    You can't do the part in red.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nkrao123@gmail. View Post
    Light used that word as it should. I thought my compiler may have that features. That why i tried. Now i understand that my compilr does not supporting this feature
    Hint: People are sending messages with more than one line and more than code samples... READ what they are sending to you!

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Newer versions of gcc let this slide with a warning. I didn't start C programming until after gcc 4.+ was released, so I'm guessing that 3.4.5 does not do that. Try compiling:

    gcc -Wall -std=c99

    You can also use "-std=gnu99" which might be slightly more compatible with the default on linux (std=gnu89).

    If that doesn't work, post the actual error you are getting.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    kotin
    Join Date
    Oct 2009
    Posts
    132
    HI light and Mk27,

    i am getting error if i compile with "gcc name.c -std=c99" and "gcc name.c -std=gnu99"

    error

    gcc 22.c -std=gnu99
    22.c: In function `main':
    22.c:6: error: variable-sized object may not be initialized
    22.c:6: warning: excess elements in array initializer
    22.c:6: warning: (near initialization for `a')
    22.c:6: warning: excess elements in array initializer
    22.c:6: warning: (near initialization for `a')



    gcc 22.c -std=c99
    22.c: In function `main':
    22.c:6: error: variable-sized object may not be initialized
    22.c:6: warning: excess elements in array initializer
    22.c:6: warning: (near initialization for `a')
    22.c:6: warning: excess elements in array initializer
    22.c:6: warning: (near initialization for `a')




    i fell my compiler may not support this feature. am i right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compilation error on array assignment.
    By sanddune008 in forum C Programming
    Replies: 2
    Last Post: 07-26-2010, 02:50 AM
  2. error: was not declared in this scope compilation error
    By i_r_tomash in forum C Programming
    Replies: 2
    Last Post: 05-27-2010, 07:44 AM
  3. Compilation Error. Need help
    By CS_Student8337 in forum C Programming
    Replies: 4
    Last Post: 02-11-2009, 10:57 AM
  4. help about compilation error
    By netwizio in forum Linux Programming
    Replies: 2
    Last Post: 01-16-2004, 06:22 PM
  5. compilation error
    By blue_gene in forum C++ Programming
    Replies: 1
    Last Post: 12-28-2003, 12:49 AM